home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Text / Password.php next >
PHP Script  |  2004-03-24  |  15KB  |  519 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // |          Olivier Vanhoucke <olivier@php.net>                         |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Password.php,v 1.7 2003/04/03 11:37:19 mj Exp $
  21. //
  22.  
  23. /**
  24.  * Number of possible characters in the password
  25.  */
  26. $_Text_Password_NumberOfPossibleCharacters = 0;
  27.  
  28. /**
  29.  * Create passwords
  30.  *
  31.  * @package Text_Password
  32.  * @author  Martin Jansen <mj@php.net>
  33.  * @author  Olivier Vanhoucke <olivier@php.net>
  34.  */
  35. class Text_Password {
  36.  
  37.     /**
  38.      * Create a single password.
  39.      *
  40.      * @access public
  41.      * @param  integer Length of the password.
  42.      * @param  string  Type of password (pronounceable, unpronounceable)
  43.      * @param  string  Character which could be use in the
  44.      *                 unpronounceable password ex : 'A,B,C,D,E,F,G'
  45.      *                 or numeric or alphanumeric.
  46.      * @return string  Returns the generated password.
  47.      */
  48.     function create($length = 10, $type = 'pronounceable', $chars = '')
  49.     {
  50.         mt_srand((double) microtime() * 1000000);
  51.  
  52.         switch ($type) {
  53.         case 'unpronounceable' :
  54.             return Text_Password::_createUnpronounceable($length, $chars);
  55.  
  56.         case 'pronounceable' :
  57.         default :
  58.             return Text_Password::_createPronounceable($length);
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * Create multiple, different passwords
  64.      *
  65.      * Method to create a list of different passwords which are
  66.      * all different.
  67.      *
  68.      * @access public
  69.      * @param  integer Number of different password
  70.      * @param  integer Length of the password
  71.      * @param  string  Type of password (pronounceable, unpronounceable)
  72.      * @param  string  Character which could be use in the
  73.      *                 unpronounceable password ex : 'A,B,C,D,E,F,G'
  74.      *                 or numeric or alphanumeric.
  75.      * @return array   Array containing the passwords
  76.      */
  77.     function createMultiple($number, $length = 10, $type = 'pronounceable', $chars = '')
  78.     {
  79.         $passwords = array();
  80.  
  81.         while ($number > 0) {
  82.             while (true) {
  83.                 $password = Text_Password::create($length, $type, $chars);
  84.                 if (!in_array($password, $passwords)) {
  85.                     $passwords[] = $password;
  86.                     break;
  87.                 }
  88.             }
  89.             $number--;
  90.         }
  91.         return $passwords;
  92.     }
  93.  
  94.     /**
  95.      * Create password from login
  96.      *
  97.      * Method to create password from login
  98.      *
  99.      * @access public
  100.      * @param  string  Login
  101.      * @param  string  Type
  102.      * @param  integer Key
  103.      * @return string
  104.      */
  105.     function createFromLogin($login, $type, $key = 0)
  106.     {
  107.         switch ($type) {
  108.         case 'reverse':
  109.             return strrev($login);
  110.  
  111.         case 'shuffle':
  112.             return Text_Password::_shuffle($login);
  113.  
  114.         case 'xor':
  115.             return Text_Password::_xor($login, $key);
  116.  
  117.         case 'rot13':
  118.             return str_rot13($login);
  119.  
  120.         case 'rotx':
  121.             return Text_Password::_rotx($login, $key);
  122.  
  123.         case 'rotx++':
  124.             return Text_Password::_rotxpp($login, $key);
  125.  
  126.         case 'rotx--':
  127.             return Text_Password::_rotxmm($login, $key);
  128.  
  129.         case 'ascii_rotx':
  130.             return Text_Password::_asciiRotx($login, $key);
  131.  
  132.         case 'ascii_rotx++':
  133.             return Text_Password::_asciiRotxpp($login, $key);
  134.  
  135.         case 'ascii_rotx--':
  136.             return Text_Password::_asciiRotxmm($login, $key);
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Create multiple, different passwords from an array of login
  142.      *
  143.      * Method to create a list of different password from login
  144.      *
  145.      * @access public
  146.      * @param  array   Login
  147.      * @param  string  Type
  148.      * @param  integer Key
  149.      * @return array   Array containing the passwords
  150.      */
  151.     function createMultipleFromLogin($login, $type, $key = 0)
  152.     {
  153.         $passwords = array();
  154.         $number    = count($login);
  155.         $save      = $number;
  156.  
  157.         while ($number > 0) {
  158.             while (true) {
  159.                 $password = Text_Password::createFromLogin($login[$save - $number], $type, $key);
  160.                 if (!in_array($password, $passwords)) {
  161.                     $passwords[] = $password;
  162.                     break;
  163.                 }
  164.             }
  165.             $number--;
  166.         }
  167.         return $passwords;
  168.     }
  169.  
  170.     /**
  171.      * Helper method to create password
  172.      *
  173.      * Method to create a password from a login
  174.      *
  175.      * @access private
  176.      * @param  string  Login
  177.      * @param  integer Key
  178.      * @return string
  179.      */
  180.     function _xor($login, $key)
  181.     {
  182.         $tmp = '';
  183.  
  184.         for ($i = 0; $i < strlen($login); $i++) {
  185.             $next = ord($login{$i}) ^ $key;
  186.             if ($next > 255) {
  187.                 $next -= 255;
  188.             } elseif ($next < 0) {
  189.                 $next += 255;
  190.             }
  191.             $tmp .= chr($next);
  192.         }
  193.  
  194.         return $tmp;
  195.     }
  196.  
  197.     /**
  198.      * Helper method to create password
  199.      *
  200.      * Method to create a password from a login
  201.      * lowercase only
  202.      *
  203.      * @access private
  204.      * @param  string  Login
  205.      * @param  integer Key
  206.      * @return string
  207.      */
  208.     function _rotx($login, $key)
  209.     {
  210.         $tmp = '';
  211.         $login = strtolower($login);
  212.  
  213.         for ($i = 0; $i < strlen($login); $i++) {
  214.             if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  215.                 $next = ord($login{$i}) + $key;
  216.                 if ($next > 122) {
  217.                     $next -= 26;
  218.                 } elseif ($next < 97) {
  219.                     $next += 26;
  220.                 }
  221.                 $tmp .= chr($next);
  222.             } else {
  223.                 $tmp .= $login{$i};
  224.             }
  225.         }
  226.  
  227.         return $tmp;
  228.     }
  229.  
  230.     /**
  231.      * Helper method to create password
  232.      *
  233.      * Method to create a password from a login
  234.      * lowercase only
  235.      *
  236.      * @access private
  237.      * @param  string  Login
  238.      * @param  integer Key
  239.      * @return string
  240.      */
  241.     function _rotxpp($login, $key)
  242.     {
  243.         $tmp = '';
  244.         $login = strtolower($login);
  245.  
  246.         for ($i = 0; $i < strlen($login); $i++, $key++) {
  247.             if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  248.                 $next = ord($login{$i}) + $key;
  249.                 if ($next > 122) {
  250.                     $next -= 26;
  251.                 } elseif ($next < 97) {
  252.                     $next += 26;
  253.                 }
  254.                 $tmp .= chr($next);
  255.             } else {
  256.                 $tmp .= $login{$i};
  257.             }
  258.         }
  259.  
  260.         return $tmp;
  261.     }
  262.  
  263.     /**
  264.      * Helper method to create password
  265.      *
  266.      * Method to create a password from a login
  267.      * lowercase only
  268.      *
  269.      * @access private
  270.      * @param  string  Login
  271.      * @param  integer Key
  272.      * @return string
  273.      */
  274.     function _rotxmm($login, $key)
  275.     {
  276.         $tmp = '';
  277.         $login = strtolower($login);
  278.  
  279.         for ($i = 0; $i < strlen($login); $i++, $key--) {
  280.             if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  281.                 $next = ord($login{$i}) + $key;
  282.                 if ($next > 122) {
  283.                     $next -= 26;
  284.                 } elseif ($next < 97) {
  285.                     $next += 26;
  286.                 }
  287.                 $tmp .= chr($next);
  288.             } else {
  289.                 $tmp .= $login{$i};
  290.             }
  291.         }
  292.  
  293.         return $tmp;
  294.     }
  295.  
  296.     /**
  297.      * Helper method to create password
  298.      *
  299.      * Method to create a password from a login
  300.      *
  301.      * @access private
  302.      * @param  string  Login
  303.      * @param  integer Key
  304.      * @return string
  305.      */
  306.     function _asciiRotx($login, $key)
  307.     {
  308.         $tmp = '';
  309.  
  310.         for ($i = 0; $i < strlen($login); $i++) {
  311.             $next = ord($login{$i}) + $key;
  312.             if ($next > 255) {
  313.                 $next -= 255;
  314.             } elseif ($next < 0) {
  315.                 $next += 255;
  316.             }
  317.             $tmp .= chr($next);
  318.         }
  319.  
  320.         return $tmp;
  321.     }
  322.  
  323.     /**
  324.      * Helper method to create password
  325.      *
  326.      * Method to create a password from a login
  327.      *
  328.      * @access private
  329.      * @param  string  Login
  330.      * @param  integer Key
  331.      * @return string
  332.      */
  333.     function _asciiRotxpp($login, $key)
  334.     {
  335.         $tmp = '';
  336.  
  337.         for ($i = 0; $i < strlen($login); $i++, $key++) {
  338.             $next = ord($login{$i}) + $key;
  339.             if ($next > 255) {
  340.                 $next -= 255;
  341.             } elseif ($next < 0) {
  342.                 $next += 255;
  343.             }
  344.             $tmp .= chr($next);
  345.         }
  346.  
  347.         return $tmp;
  348.     }
  349.  
  350.     /**
  351.      * Helper method to create password
  352.      *
  353.      * Method to create a password from a login
  354.      *
  355.      * @access private
  356.      * @param  string  Login
  357.      * @param  integer Key
  358.      * @return string
  359.      */
  360.     function _asciiRotxmm($login, $key)
  361.     {
  362.         $tmp = '';
  363.  
  364.         for ($i = 0; $i < strlen($login); $i++, $key--) {
  365.             $next = ord($login{$i}) + $key;
  366.             if ($next > 255) {
  367.                 $next -= 255;
  368.             } elseif ($next < 0) {
  369.                 $next += 255;
  370.             }
  371.             $tmp .= chr($next);
  372.         }
  373.  
  374.         return $tmp;
  375.     }
  376.  
  377.     /**
  378.      * Helper method to create password
  379.      *
  380.      * Method to create a password from a login
  381.      *
  382.      * @access private
  383.      * @param  string  Login
  384.      * @return string
  385.      */
  386.     function _shuffle($login)
  387.     {
  388.         $tmp = array();
  389.  
  390.         for ($i = 0; $i < strlen($login); $i++) {
  391.             $tmp[] = $login{$i};
  392.         }
  393.  
  394.         shuffle($tmp);
  395.  
  396.         return implode($tmp, '');
  397.     }
  398.  
  399.     /**
  400.      * Create pronounceable password
  401.      *
  402.      * This method creates a string that consists of
  403.      * vowels and consonats.
  404.      *
  405.      * @access private
  406.      * @param  integer Length of the password
  407.      * @return string  Returns the password
  408.      */
  409.     function _createPronounceable($length)
  410.     {
  411.  
  412.         global $_Text_Password_NumberOfPossibleCharacters;
  413.         $retVal = '';
  414.  
  415.         /**
  416.          * List of vowels and vowel sounds
  417.          */
  418.         $v = array('a', 'e', 'i', 'o', 'u', 'ae', 'ou', 'io',
  419.                    'ea', 'ou', 'ia', 'ai'
  420.                    );
  421.  
  422.         /**
  423.          * List of consonants and consonant sounds
  424.          */
  425.         $c = array('b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm',
  426.                    'n', 'p', 'r', 's', 't', 'u', 'v', 'w',
  427.                    'tr', 'cr', 'fr', 'dr', 'wr', 'pr', 'th',
  428.                    'ch', 'ph', 'st', 'sl', 'cl'
  429.                    );
  430.  
  431.         $v_count = 12;
  432.         $c_count = 29;
  433.  
  434.         $_Text_Password_NumberOfPossibleCharacters = $v_count + $c_count;
  435.  
  436.         for ($i = 0; $i < $length; $i++) {
  437.             $retVal .= $c[mt_rand(0, $c_count-1)] . $v[mt_rand(0, $v_count-1)];
  438.         }
  439.  
  440.         return substr($retVal, 0, $length);
  441.     }
  442.  
  443.     /**
  444.      * Create unpronounceable password
  445.      *
  446.      * This method creates a random unpronounceable password
  447.      *
  448.      * @access private
  449.      * @param  integer Length of the password
  450.      * @param  string  Character which could be use in the
  451.      *                 unpronounceable password ex : 'A,B,C,D,E,F,G'
  452.      *                 or numeric or alphanumeric.
  453.      * @return string  Returns the password
  454.      */
  455.     function _createUnpronounceable($length, $chars)
  456.     {
  457.         global $_Text_Password_NumberOfPossibleCharacters;
  458.  
  459.         $password = '';
  460.  
  461.         /**
  462.          * List of character which could be use in the password
  463.          */
  464.          switch($chars) {
  465.  
  466.          case 'alphanumeric':
  467.              $regex = 'A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8|9';
  468.              $_Text_Password_NumberOfPossibleCharacters = 62;
  469.              break;
  470.  
  471.          case 'numeric':
  472.              $regex = '0|1|2|3|4|5|6|7|8|9';
  473.              $_Text_Password_NumberOfPossibleCharacters = 10;
  474.              break;
  475.  
  476.          case '':
  477.              $regex = '_|#|@|%|ú|&|τ|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8|9';
  478.              $_Text_Password_NumberOfPossibleCharacters = 69;
  479.              break;
  480.  
  481.          default:
  482.              /**
  483.               * Some characters shouldn't be used
  484.               */
  485.              $chars = trim($chars);
  486.              $chars = str_replace('+' , '' , $chars);
  487.              $chars = str_replace('|' , '' , $chars);
  488.              $chars = str_replace('$' , '' , $chars);
  489.              $chars = str_replace('^' , '' , $chars);
  490.              $chars = str_replace('/' , '' , $chars);
  491.              $chars = str_replace('\\', '' , $chars);
  492.              $chars = str_replace(',,', ',', $chars);
  493.  
  494.              if ($chars{strlen($chars)-1} == ',') {
  495.                  $chars = substr($chars, 0, -1);
  496.              }
  497.  
  498.              $regex = str_replace(',', '|', $chars);
  499.              $_Text_Password_NumberOfPossibleCharacters = strlen(str_replace(',', '', $chars));
  500.          }
  501.  
  502.          /**
  503.           * Generate password
  504.           */
  505.          do {
  506.              $chr = chr(mt_rand(0, 255));
  507.              if (preg_match('/'.$regex.'/US', $chr)) {
  508.                  $password .= $chr;
  509.              }
  510.          } while (strlen($password) < $length);
  511.  
  512.          /**
  513.           * Return password
  514.           */
  515.          return $password;
  516.     }
  517. }
  518. ?>
  519.